package com.jjb.www.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.bar.TitleBar;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.http.api.CurriculumSortApi;
import com.jjb.www.http.api.DryRrunApi;
import com.jjb.www.http.api.UserQuesetionDeleteApi;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.ui.adapter.DryRunAdapter;
import com.jjb.www.ui.dialog.MessageDialog;
import com.jjb.www.ui.fragment.CourseListFragment;
import com.jjb.www.widget.CommonRefreshView;

import java.util.List;

import okhttp3.Call;

/**
 * 模考记录
 */
public class DryRrunActivity extends AppActivity implements BaseAdapter.OnItemClickListener,BaseAdapter.OnChildClickListener {
    private CommonRefreshView mRefreshView;
    private DryRunAdapter mAdapter;
    private TitleBar mTitleBar;





    @Override
    protected int getLayoutId() {
        return R.layout.activity_dry_rrun;
    }

    @Override
    protected void initView() {
        mRefreshView = findViewById(R.id.refreshView);
        mAdapter = new DryRunAdapter(getContext());
        mTitleBar = findViewById(R.id.titleBar);
        mAdapter.setOnChildClickListener(R.id.rl_detele, this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        getListData();

    }

    private void getListData() {
        mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<DryRrunApi.Bean>() {
            @Override
            public AppAdapter<DryRrunApi.Bean> getAdapter() {
                return mAdapter;
            }

            @Override
            public void loadData(int p, HttpCallback<HttpListData<DryRrunApi.Bean>> callback) {
                EasyHttp.get(DryRrunActivity.this)
                        .api(new DryRrunApi()
                                .setPage(p)
                                .setPageSize(mAdapter.getSize())
                        ).request(new HttpCallback<HttpListData<DryRrunApi.Bean>>(DryRrunActivity.this) {

                    @Override
                    public void onStart(Call call) {

                    }

                    @Override
                    public void onSucceed(HttpListData<DryRrunApi.Bean> result) {
                        callback.onSucceed(result);
                        if (result.getData().getList().size()>0){
                            mTitleBar.setTitle("模考记录("+result.getData().getList().size()+")");
                        }

                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        callback.onFail(e);
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        callback.onEnd(call);
                    }
                });
            }

            @Override
            public void onRefreshSuccess(List<DryRrunApi.Bean> list, int listCount) {

            }

            @Override
            public void onRefreshFailure() {

            }

            @Override
            public void onLoadMoreSuccess(List<DryRrunApi.Bean> loadItemList, int loadItemCount) {

            }

            @Override
            public void onLoadMoreFailure() {

            }
        });
        mRefreshView.initData();
    }

    @Override
    public void onChildClick(RecyclerView recyclerView, View childView, int position) {
        new MessageDialog.Builder(getContext())
                .setMessage("您是否要删除此次模考记录？")
                .setConfirm("删除记录")
                .setCancel("再想想")
                .setCancelable(false)
                .setListener(new MessageDialog.OnListener() {

                    @Override
                    public void onConfirm(BaseDialog dialog) {
                        deteleData(mAdapter.getItem(position).getId()+"");
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                    }
                })
                .show();
    }

    private void deteleData(String id) {
        EasyHttp.get(this)
                .api(new UserQuesetionDeleteApi()
                .setPaper_id(id))
                .request(new HttpCallback<HttpData<UserQuesetionDeleteApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<UserQuesetionDeleteApi.Bean> data) {
                        getListData();
                    }

                });
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        Intent intent = new Intent();
        intent.putExtra("id",mAdapter.getItem(position).getId()+"");
        intent.setClass(getContext(),AnsweringDetailsActivity.class);
        startActivity(intent);
    }
}